import React, {Component} from 'react';
import PropTypes from 'prop-types';

class SshForm extends Component {
    constructor() {
        super()
        this.validFnArr = []
        this.clearValidFnArr = []
    }

    getChildContext() {
        return {
            collectValidFns: (fn) => {
                this.validFnArr.push(fn)
            },
            collectClearValidFns: (fn) => {
                this.clearValidFnArr.push(fn)
            }
        };
    }

    valid = (callback) => {
        let resolveNum = 0
        this.validFnArr.forEach(fn => {
            fn()
                .then(() => {
                    resolveNum++
                    if (resolveNum === this.validFnArr.length) {
                        callback()
                    }
                })
                .catch(() => {

                })
        })
    }

    clearValid = () => {
        this.clearValidFnArr.forEach(fn => {
            fn()
        })
    }

    render() {
        return (
            <form>
                {this.props.children}
            </form>
        )
    }
}

SshForm.childContextTypes = {
    collectValidFns: PropTypes.func,
    collectClearValidFns: PropTypes.func
};
export default SshForm
